<?php

namespace app\ports\controller;

use app\ports\constants\JumpConstants;
use app\ports\controller\publics\Publics;
use think\Log;

/**
 * 交易|商户通知地址管理
 * Class  Notic
 * @package app\ports\controller
 */
class Notic
{

	/**
	 * 盛付通
	 */
	public function sft_trans()
	{
		$parms = request()->param();
		$ret = ["respMsg" => "失败", "respCode" => '00'];
		try {
			empty($parms) && exit(json_encode(["respMsg" => "成功", "respCode" => "00"], JSON_UNESCAPED_UNICODE));
			if (in_array($parms["busTyp"], ['3', '4']))  exit(json_encode(["respMsg" => "成功", "respCode" => "00"], JSON_UNESCAPED_UNICODE));
			if (!empty($parms)) {
				unset($parms["sign"]);
				$sn = in_array($parms["busTyp"],  [2, 5]) ?  $parms["sn"] : $parms["snNo"];
				Publics::instance()->sends_notic($parms, JumpConstants::PATENER_NEW_TRANS_SFT, $sn);
				$ret = ["respMsg" => "成功", "respCode" => "00"];
			}
		} catch (\Exception $e) {
			echo $e->getMessage();
		}
		echo json_encode($ret, JSON_UNESCAPED_UNICODE);
	}

	/**
	 * 海科交易通知
	 */
	public function hk_trans()
	{
		$parms = request()->param();
		$returnStr = "RECV_ORD_ID_UNKNOWN";
		if (!empty($parms)) {
			try {
				$jsonData = json_decode($parms["jsonData"], true);
				if (!empty($jsonData)) {
					$orderDataList = json_decode($jsonData["orderDataList"], true);
					if (!empty($orderDataList)) {
						foreach ($orderDataList as $vals) {
							if ($vals["ttxnSts"] == 'S') {
								if ($vals["crdFlg"] == " " || empty($vals["crdFlg"]) || $vals["crdFlg"] == "null") {
									$vals["crdFlg"] = "06";
								}
								$temp = [];
								// 数组中大写键值对 清除
								foreach ($vals as $key => $values) {
									if (!ctype_upper($key[0])) {
										$temp[$key] = $values;
									}
								}
								Publics::instance()->sends_notic($temp, JumpConstants::PATENER_NEW_TRANS_HK, $temp["snNo"]);
							}
						}
					}
				}
				$returnStr = "RECV_ORD_ID_" . $jsonData["extSeqId"];
			} catch (\Exception $e) {
				echo $e->getMessage();
			}
		}
		echo $returnStr;
	}

	/**
	 * 海科商户通知
	 */
	public function hk_merchant()
	{
		$parms = request()->param();
		$returnStr = "RECV_ORD_ID_UNKNOWN";
		if (!empty($parms)) {
			try {
				$data = json_decode($parms["jsonData"], true);
				if (!empty($data)) {
					$orderDataList = json_decode($data["orderDataList"], true);
					if ($orderDataList) {
						for ($i = 0; $i < count($orderDataList); $i++) {
							$temp = [];
							// 数组中大写键值对 清除
							foreach ($orderDataList[$i] as $key => $values) {
								if (!ctype_upper($key[0])) {
									$temp[$key] = $values;
								}
							}
							Publics::instance()->sends_notic($temp, JumpConstants::PATENER_NEW_BINDS_HK, $temp["snNo"]);
						}
					}
					$returnStr = "RECV_ORD_ID_" . $data["extSeqId"];
				}
			} catch (\Exception $e) {
				Log::write($e->getMessage());
			}
		}
		echo $returnStr;
	}
}
